<template>
   <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">

      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <template #header>
        <div class="header">
           <span>共{{total}}条数据</span>
          <el-button @click="openDrawer('add')" icon="el-icon-plus" size="small" type="primary" round>添加面经</el-button>
        </div>
      </template>

      <el-table
        :data="list"
        stripe
        style="width: 100%">
       <el-table-column prop="stem" label="标题" width="400">
          </el-table-column>
          <el-table-column prop="creator" label="作者"> </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间" width="200">
          </el-table-column>
          <el-table-column label="操作">
              <template #default="scope">
                <div class="actions">
                  <i class="el-icon-view" @click="openDrawer('view',scope.row.id)"></i>
                  <i class="el-icon-edit" @click="openDrawer('edit',scope.row.id)"></i>
                  <i class="el-icon-delete" @click="handleDel(scope.row.id)"></i>
                </div>
              </template>
            </el-table-column>

      </el-table>
      <el-drawer
        :title="title"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleClose"
        size="50%"
        >
        <div v-if="type === 'view'" class="article-preview">
          <h5>{{ drawerForm.stem }}</h5>
          <div v-html="drawerForm.content"></div>
        </div>
        <el-form ref="drawerForm" label-width="80px" :rules="rules" :model="drawerForm" v-else>
          <el-form-item label="标题" prop="stem">
            <el-input  placeholder="输入面经标题" v-model="drawerForm.stem"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <quill-editor v-model="drawerForm.content"></quill-editor>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">确认</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>

      </el-drawer>
      <el-pagination
          background
          :total='total'
          :page-size='p.pageSize'
          layout="prev, pager, next"
          @current-change="changeCurrent"
          >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { createArticle, delArticle, getArticleDetial, getArticles, updateArticle } from '@/api/article'
export default {
  name: 'article-page',
  components: { quillEditor },
  data () {
    return {
      list: [],
      p: {
        current: 1,
        pageSize: 5
      },

      total: 100,
      drawer: false,
      direction: 'rtl',
      type: 'add',
      drawerForm: {
        stem: '我阙大算法工程师持标拿键，谁敢和我抢工作？？' + Date.now(),
        content: '阙毅牛逼！'
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入面经内容', trigger: 'blur' }]

      }
    }
  },
  async created () {
    this.showArticle()
    // setInterval(() => {
    //   this.openDrawer('add')
    //   this.submit()
    //   location.reload()
    // }, 200)
  },
  computed: {
    title () {
      if (this.type === 'add') {
        return '添加面经'
      } else if (this.type === 'edit') {
        return '修改面经'
      } else {
        return '面经预览'
      }
    }
  },

  methods: {
    changeCurrent (val) {
      this.p.current = val
      this.showArticle()
    },
    async showArticle () {
      const res = await getArticles(this.p)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      this.pageTotal = res.data.pageTotal
    },
    async  handleDel (id) {
      // console.log(id)
      await delArticle({ id })
      // console.log(res)
      this.showArticle()
      this.$message.success('删除成功')
    },
    async openDrawer (type, id) {
      this.type = type
      this.drawer = true
      if (this.type === 'edit' || this.type === 'view') {
        const res = await getArticleDetial({ id })
        // console.log(res)
        this.drawerForm = { ...res.data }
        // console.log(this.drawerForm)
      } else {
        // this.drawerForm = {
        //   stem: '',
        //   content: ''
        // }
      }
    },
    submit () {
      this.$refs.drawerForm.validate(async (valid) => {
        if (!valid) return
        if (this.type === 'add') {
          // console.log(this.drawerForm)
          await createArticle(this.drawerForm)
          // this.p.current = 1
          // this.showArticle()
          // this.handleClose()
          // this.$message.success('添加成功')
        }
        if (this.type === 'edit') {
          this.drawerForm = {
            id: this.drawerForm.id,
            stem: this.drawerForm.stem,
            content: this.drawerForm.content
          }
          await updateArticle(this.drawerForm)
          this.showArticle()
          this.handleClose()
          this.$message.success('修改成功')
        }
      })
    },
    handleClose () {
      this.drawer = false
      this.drawerForm = {
        stem: '',
        content: ''
      }
      if (this.type !== 'view') {
        this.$refs.drawerForm.resetFields()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}

</style>
